﻿from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import ObjectPaginator, InvalidPage
from app.engine import models
from google.appengine.api import users
from google.appengine.ext import db
from app.functions.funcs import render,split_tags,formate_time,gen_duration
from google.appengine.api import memcache
from django.template import loader, Context
from parsing import xmlData

def adminConsole(request):
    user = users.get_current_user()
    if users.is_current_user_admin():
        items = models.Card.gql("ORDER BY category desc,number asc")
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict(items=items)
    return render('admin.html', payload)

def adminAddItem(request):
    user = users.get_current_user()
    if users.is_current_user_admin():
         if request.method == 'POST':
            item = models.Card(title = request.POST.get('title').decode('utf-8'),
                               identity = request.POST.get('identity').decode('utf-8'),
                               number = request.POST.get('number').decode('utf-8'),
                               category = request.POST.get('category').decode('utf-8'),
                               quantity = request.POST.get('quantity').decode('utf-8'),)
            item.put()
            return HttpResponseRedirect('/admin/')
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict()
    return render('admin_item.html', payload)

def adminEditItem(request,item_id):
    user = users.get_current_user()
    if users.is_current_user_admin():
        item = models.Card.get_by_id(int(item_id))
        if request.method == 'POST':
            item.title = request.POST.get('title').decode('utf-8')
            item.identity = request.POST.get('identity').decode('utf-8')
            item.number = request.POST.get('number').decode('utf-8')
            item.category = request.POST.get('category').decode('utf-8')
            item.quantity = request.POST.get('quantity').decode('utf-8')
            item.put()
            return HttpResponseRedirect('/admin/')
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict(item=item)
    return render('admin_item.html', payload)

def adminDelItem(request,item_id):
    user = users.get_current_user()
    if users.is_current_user_admin():
        item = models.Card.get_by_id(int(item_id))
        return HttpResponseRedirect('/admin/')
    else:
        return HttpResponseRedirect(users.create_login_url("/"))

def importItemsDB(request):
    user = users.get_current_user()
    if users.is_current_user_admin():
        if request.method == 'POST':
            data = request.POST.get('data')
            rawdata = xmlData(data)
            rawdata.getItemInfo()
            for item in rawdata.itemsdb:
                item_template = models.Card(title=item['title'],
                                            identity=item['identity'],
                                            number=item['number'],
                                            category=item['category'],
                                            quantity=item['quantity'])
                item = models.Card.all().filter('identity =',item_template.identity)
                if item.count() == 0:
                    item_template.put()
            return HttpResponseRedirect('/admin/')
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict()
    return render('admin_import_items.html', payload)

def exportXML(request):
    items = models.Card.all()
    response = HttpResponse(mimetype='application/x-python')
    response['Content-Disposition'] = 'attachment;filename=%s.xml' %'data'
    t = loader.get_template('export_xml.html')
    c = Context({
        'items': items,
    })
    response.write(t.render(c))
    return response
